package cn.lbd.zk;

import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class ZKGet {
    private CountDownLatch countDownLatch = null;
    private ZooKeeper zooKeeper = null;
    private String socketAddress = "39.97.191.1:2181";

    @Before
    public void init() throws Exception {
        countDownLatch = new CountDownLatch(1);
        zooKeeper = new ZooKeeper(socketAddress, 5000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                if (event.getState() == Event.KeeperState.SyncConnected) {
                    System.out.println("连接建立成功");
                    //连接成功，通知计数器-1
                    countDownLatch.countDown();
                    System.out.println(event.getState());
                    System.out.println(event.getType());
                }
            }
        });
        countDownLatch.await();
        System.out.println("会话ID => " + zooKeeper.getSessionId());
    }

    @After
    public void destroy() throws InterruptedException {
        zooKeeper.close();
    }

    //同步
    @Test
    public void test1() throws KeeperException, InterruptedException {
        Stat stat = new Stat();
        byte[] data = zooKeeper.getData("/create/demo1", false, stat);
        System.out.println(new String(data));
        System.out.println(stat.getVersion());
    }


    //异步
    @Test
    public void test2() throws InterruptedException {
        zooKeeper.getData("/create/demo1", false, new AsyncCallback.DataCallback() {
            @Override
            public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
                if (rc == 0) {
                    System.out.println("创建节点成功");
                } else {
                    System.out.println("创建节点失败");
                }
                System.out.println("节点路径" + path);
                System.out.println(ctx);
                System.out.println("节点数据" + new String(data));
                System.out.println("数据版本号" + stat.getVersion());
            }
        }, "这是一个上下文参数");
        TimeUnit.SECONDS.sleep(3);
        System.out.println("结束");
    }

    //同步监听
    @Test
    public void test3() throws KeeperException, InterruptedException {
        Stat stat = new Stat();
        byte[] data = zooKeeper.getData("/create/demo20", true, stat);
        System.out.println("节点数据" + new String(data));
        TimeUnit.SECONDS.sleep(50);
    }

    @Test
    public void test4() throws KeeperException, InterruptedException {
        byte[] data = zooKeeper.getData("/create/demo20", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("自定义");
                System.out.println(event.getState());
                System.out.println(event.getType());
            }
        }, null);
        System.out.println("节点数据" + new String(data));
        TimeUnit.SECONDS.sleep(50);
    }

    @Test
    public void test5() throws KeeperException, InterruptedException {
        byte[] data = zooKeeper.getData("/create/demo20", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("自定义1");
                System.out.println(event.getState());
                System.out.println(event.getType());
            }
        }, null);
        System.out.println("节点数据" + new String(data));
        byte[] data1 = zooKeeper.getData("/create/demo20", new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println("自定义2");
                System.out.println(event.getState());
                System.out.println(event.getType());
            }
        }, null);
        System.out.println("节点数据" + new String(data1));
        TimeUnit.SECONDS.sleep(50);
    }

    @Test
    public void test6() throws KeeperException, InterruptedException {
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                System.out.println(event.getState());
                System.out.println(event.getType());
                try {
                    zooKeeper.getData("/create/demo20",this,null);
                } catch (KeeperException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        };
        byte[] data = zooKeeper.getData("/create/demo20", watcher, null);
        System.out.println("节点数据" + new String(data));
        TimeUnit.SECONDS.sleep(50);
    }
}
